﻿using DayuCode.Designer.BLL;
using DayuCode.Designer.Entity;
using DayuCode.Designer.Service;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DayuCode.Designer.Forms
{
    public partial class FormProjectSelect : UIForm
    {
        public FormProjectSelect()
        {
            InitializeComponent();
            projectTree.Nodes.Clear();
        }

        /// <summary>
        /// 加载子节点
        /// </summary>
        private void fillChildNodes(TreeNode node)
        {
            long catalogId = -1;
            bool loadChild = false;
            if(node != null)
            {
                var catalog = node.Tag as ProjectCatalog;
                if (catalog != null)
                {
                    catalogId = catalog.Id;
                    if (node.Nodes.Count == 1)
                    {
                        var cnode = node.Nodes[0];
                        if ("tmp".Equals(cnode.Tag))
                        {
                            cnode.Remove();
                            loadChild = true;
                        }
                    }
                }
            }
            else
            {
                loadChild = true;
            }
            if (loadChild)
            {
                var catalogs = ProjectCatalogHandler.getChildProjectCatalogs(catalogId);
                var projects = ProjectHandler.getProjects(catalogId);

                foreach (var catalog in catalogs)
                {
                    TreeNode catalogNode;
                    if (node == null)
                    {
                        catalogNode = projectTree.Nodes.Add(catalog.Name) as TreeNode;
                    }
                    else
                    {
                        catalogNode = node.Nodes.Add(catalog.Name) as TreeNode;
                    }
                    catalogNode.Tag = catalog;
                    catalogNode.ImageIndex = 0;
                    catalogNode.SelectedImageIndex = 0;
                    catalogNode.Nodes.Add("tmp").Tag = "tmp";
                }
                foreach (var project in projects)
                {
                    TreeNode projectNode;
                    if (node == null)
                    {
                        projectNode = projectTree.Nodes.Add(project.Name) as TreeNode;
                    }
                    else
                    {
                        projectNode = node.Nodes.Add(project.Name) as TreeNode;
                    }
                    projectNode.Tag = project;
                    projectNode.ImageIndex = 1;
                    projectNode.SelectedImageIndex = 1;
                }
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            Project project = projectTree.SelectedNode.Tag as Project;
            if (project != null)
            {
                UserConfig.Instance.AppId = project.Id;
                UserConfig.SaveConfig(UserConfig.Instance);
                Close();
            }
            else
            {
                this.ShowErrorTip("请选择一个应用系统");
            }
        }

        private void FormProjectSelect_Load(object sender, EventArgs e)
        {
            fillChildNodes(null);
        }

        private void projectTree_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            fillChildNodes(e.Node);
        }
    }
}
